package com.hanyizhu.demostore.controller;

import com.hanyizhu.demostore.controller.ex.*;
import com.hanyizhu.demostore.service.ex.*;
import com.hanyizhu.demostore.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpSession;
import java.rmi.ServerException;

/**控制层类的基类
 * @author lenovo*/
public class BaseController {
    /**操作成功的状态码*/
    @Autowired
    public static final int OK = 200;
    public static final String REGMESSAGE = "注册成功";
    public static final String LOGINMESSAGE = "登陆成功";
    public static final String CHANGEPASSWORDSUCCESS = "密码修改成功";
    public static final String CHANGEINFOSUCCESS = "修改成功";
    public static final String CHANGEAVATAR = "上传成功";
    public static final String ADDRESSSUCCESS = "添加成功";


    /**
     * 请求处理方法:这个方法的返回值就是需要传递给前端的数据
     * 自动将异常对象传递给此方法的参数列表上
     * 当前项目中产生了相应异常,都会被拦截到此方法中,这个方法此时就会冲当的请求处理方法,方法的返回值返回前端
     */
    /**操作失败*/
    @ExceptionHandler({ServiceException.class,FileUploadIOException.class})//用于同一处理抛出的异常
    public JsonResult<Void> handleException(Throwable e){
        JsonResult<Void> result = new JsonResult<>(e);
        if (e instanceof UsernameDuplicatedException){
            result.setState(4000);
            result.setMessage("用户名被占用");
        }
        if (e instanceof AddressCountLimitException){
            result.setState(4003);
            result.setMessage("用户收货地址超出上限");
        }
        if (e instanceof AddressNotFoundException){
            result.setState(4004);
            result.setMessage("用户数据不存在");
        }
        if (e instanceof AccessDeniedException){
            result.setState(4005);
            result.setMessage("收货地址非法访问异常");
        }
        if (e instanceof InserException){
            result.setState(5000);
            result.setMessage("注册时产生未知的异常");
        }
        if (e instanceof UsernameNotFoundException){
            result.setState(5001);
            result.setMessage("用户信息不存在的异常");
        }
        if (e instanceof ProuductNoFoundException){
            result.setState(7001);
            result.setMessage("商品信息不存在的异常");
        }
        if (e instanceof CartNotFoundException){
            result.setState(8001);
            result.setMessage("购物车信息不存在的异常");
        }
        if (e instanceof OrderItemNotFoundException){
            result.setState(9001);
            result.setMessage("订单信息不存在信息不存在的异常");
        }
        if (e instanceof PasswordNotMatchException){
            result.setState(5002);
            result.setMessage("用户名的密码错误异常");
        }
        if (e instanceof UpdateException){
            result.setState(5003);
            result.setMessage("更新数据产生的未知异常");
        }
        if (e instanceof FileEmptyException){
            result.setState(6000);
            result.setMessage("文件为空异常");
        }
        if (e instanceof FileSizeException){
            result.setState(6001);
            result.setMessage("文件大小异常");
        }
        if (e instanceof FileTypeException){
            result.setState(6002);
            result.setMessage("文件类型异常");
        }
        if (e instanceof FileStateException){
            result.setState(6003);
            result.setMessage("文件状态异常");
        }
        if (e instanceof FileUploadIOException){
            result.setState(6004);
            result.setMessage("文件流异常");
        }

        return result;
    }

    /**
     * 获取session对象中的uid
     * @param session session对象
     * @return 当前登陆的用户uid的值
     */
    protected final Integer getUidFromSession(HttpSession session){
       return Integer.valueOf(session.getAttribute("uid").toString());
    }

    /**
     * 获取当前登陆用户的username
     * @param session session对象
     * @return 当前登陆用户的用户名
     */
    protected final String getUsernameFromSession(HttpSession session){
       return session.getAttribute("username").toString();
    }
}
